package br.com.dataphonebr.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.ListView;
import br.com.dataphonebr.R;
import br.com.dataphonebr.service.GenericAdapterService;

public class ActivityUtil<Entity> {

	protected Util util = new Util();
	protected GenericAdapterService<Entity> adapter;
	protected int dataSelected;
	protected AlertDialog alertDialog;
	protected AlertDialog alertYesNo;
	protected Activity instance;

	public ActivityUtil(Activity instance) {
		this.instance = instance;
	}

	public void openView(Context context, Class<?> clazz, boolean finish) {
		instance.startActivity(new Intent(context, clazz));
		if (finish)
			instance.finish();
	}

	// protected SharedPreferences getPreferences(Context context){
	// SharedPreferences preferences =
	// PreferenceManager.getDefaultSharedPreferences(context);
	// return preferences;
	// }

	public ListView newListView(int id) {
		ListView listView = (ListView) instance.findViewById(id);
		return listView;
	}

	public AlertDialog createDialog(String title, OnClickListener event,
			CharSequence... options) {
		final CharSequence[] items = options;
		AlertDialog.Builder builder = new AlertDialog.Builder(instance);
		builder.setTitle(title);
		builder.setItems(items, event);
		return builder.create();
	}

	public CharSequence[] getYesNo() {
		final String yes = instance.getString(R.string.alert_yes);
		final String no = instance.getString(R.string.alert_no);
		final CharSequence[] yesNo = { yes, no };
		return yesNo;
	}

	public boolean internetIsEnable() {
		ConnectivityManager cn = (ConnectivityManager) instance.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo i = cn.getActiveNetworkInfo();
		if (i == null)
			return false;
		if (!i.isConnected())
			return false;
		if (!i.isAvailable())
			return false;
		return true;
	}

}
